//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.swingui.editpane;

import javax.swing.JCheckBox;
import javax.swing.JTextField;

import org.jfab.core.model.CGField;

/**
 * Provides an edit pane for a code generator field.
 */
public final class CGFieldEditPane extends CGObjectEditPane<CGField>
{
    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    /** getter attribute widget. */
    private JCheckBox _getterUI;

    /** Initial value widget. */
    private JTextField _initialValueUI;

    /** setter attribute widget. */
    private JCheckBox _setterUI;

    /** Type widget. */
    private JTextField _typeUI;

    /** usedInIdentity attribute widget. */
    private JCheckBox _usedInIdentityUI;

    /**
     * Construct this object with the given parameter.
     * 
     * @param cgField Code generator field.
     */
    public CGFieldEditPane(final CGField cgField)
    {
        super(cgField);
    }

    @Override
    public final void applyChanges()
    {
        super.applyChanges();

        final CGField subject = getSubjectObject();

        subject.setGetter(_getterUI.isSelected());
        subject.setSetter(_setterUI.isSelected());
        subject.setUsedInIdentity(_usedInIdentityUI.isSelected());
        subject.setType(getCgTypeAdapter().unmarshal(_typeUI.getText()));
        subject.setInitialValue(_initialValueUI.getText());
    }

    @Override
    protected final void createWidgets()
    {
        super.createWidgets();

        _getterUI = createCheckBox();
        _initialValueUI = createTextField();
        _setterUI = createCheckBox();
        _typeUI = createTextField();
        _usedInIdentityUI = createCheckBox();
    }

    @Override
    protected final void fillPanel()
    {
        // Custom layout to resemble the code view.
        bindAndAdd("title", _titleUI);
        bindAndAdd("class", _classUI);
        bindAndAdd("referencedTypes", _referencedTypesUI,
                getCgTypeListAdapter());
        bindAndAdd("commentLines", _commentLinesUI, getStringListAdapter());
        bindAndAdd("annotations", _annotationsUI, getCgAnnotationListAdapter());
        bindAndAdd("modifiers", _modifiersUI, getStringListAdapter());
        bindAndAdd("type", _typeUI, getCgTypeAdapter());
        bindAndAdd("name", _nameUI);
        bindAndAdd("initialValue", _initialValueUI);

        bindAndAdd("getter", _getterUI);
        bindAndAdd("setter", _setterUI);
        bindAndAdd("usedInIdentity", _usedInIdentityUI);
    }

    @Override
    protected final void fillWidgets()
    {
        super.fillWidgets();

        final CGField subject = getSubjectObject();

        setValue(_getterUI, subject.isGetter());
        setValue(_setterUI, subject.isSetter());
        setValue(_usedInIdentityUI, subject.isUsedInIdentity());
        _typeUI.setText(getCgTypeAdapter().marshal(subject.getType()));
        _initialValueUI.setText(subject.getInitialValue());
    }
}
